#include "hls_fuction.h"

hls_fuction::hls_fuction()
{
    //ctor
}

hls_fuction::~hls_fuction()
{
    //dtor
}

void hls_fuction::Resize(const char* filename)
{
    IplImage* img = cvLoadImage(filename);
    IplImage* dst = cvCreateImage(cvSize(img->width - 50, img->height - 50), img -> depth, img->nChannels);
    IplImage* dst1 = cvCreateImage(cvSize(img->width + 50, img->height + 50), img -> depth, img->nChannels);
    cvShowImage("Input", img);

    cvResize(img, dst, CV_INTER_NN); //tham số cuối cùng là phương pháp nội suy
    cvResize(img, dst1, CV_INTER_LINEAR);
    //cvResize(img, dst, CV_INTER_LINEAR);
    //cvResize(img, dst, CV_INTER_AREA);
    //cvResize(img, dst, CV_INTER_CUBIC);
    cvNamedWindow("dst", CV_WINDOW_AUTOSIZE);
    cvShowImage("dst", dst);

    cvNamedWindow("dst1", CV_WINDOW_AUTOSIZE);
    cvShowImage("dst1", dst1);

    // wait for user
    cvWaitKey(0);
}

void hls_fuction::FloodFill1(const char* filename)
{
    cv::Mat image = cv::imread(filename, -1);
    cv::imshow("image before filling", image);
    int filling = cv::floodFill(image, cv::Point(0,0), 255, (cv::Rect*)0, cv::Scalar(), 200);
    cv::imshow("image after filling", image);
    cv::waitKey();
}

void hls_fuction::FloodFill(const char* filename, const char* filename2)
{
    IplImage* img = cvLoadImage(filename);
    IplImage* img1 = cvLoadImage(filename2);
    //IplImage* dst = cvCreateImage(cvSize(img->width - 20, img->height - 20), img -> depth, img->nChannels);
    cvShowImage("Input", img);
    cvShowImage("Input1", img1);

    // define the seed point
    CvPoint seedPoint = cvPoint(80,80);
    CvPoint seedPoint1 = cvPoint(200,200);

    //cvResize(img, dst, CV_INTER_NN);

    // flood fill with red
    cvFloodFill(img, seedPoint, CV_RGB(255,255,0), CV_RGB(8,90,60), CV_RGB(10,100,70),NULL,4,NULL);

    cvFloodFill(img1, seedPoint1, CV_RGB(255,0,0), CV_RGB(255,255,255), CV_RGB(10,100,70),NULL,4,NULL);

    // draw a blue circle at the seed point
    cvCircle(img, seedPoint, 3, CV_RGB(0,0,255), 3, 8);
    cvCircle(img1, seedPoint1, 3, CV_RGB(0,255,0), 2, 3);

    // show the output
    cvNamedWindow("Output", CV_WINDOW_AUTOSIZE);
    cvNamedWindow("Output1", CV_WINDOW_AUTOSIZE);
    //cvNamedWindow("dst", CV_WINDOW_AUTOSIZE);
    cvShowImage("Output", img);
    cvShowImage("Output1", img1);
    //cvShowImage("dst", dst);

    // wait for user
    cvWaitKey(0);
}

void hls_fuction::HistogramEqual_GrayScale(const char* filename)
{
    Mat img = imread(filename, CV_LOAD_IMAGE_COLOR);
    cvtColor(img, img, CV_BGR2GRAY);
    Mat imghist;
    equalizeHist(img, imghist);
    namedWindow("ogr", CV_WINDOW_AUTOSIZE);
    imshow("ogr", img);
    namedWindow("Hist", CV_WINDOW_AUTOSIZE);
    imshow("Hist", imghist);
    waitKey(0);
    destroyAllWindows();
}

void hls_fuction::HistogramEqual_ColorImage(const char* filename)
{
    Mat img = imread(filename, CV_LOAD_IMAGE_COLOR);
    vector<Mat> channels;
    Mat img_hist_equalized;

    cvtColor(img, img_hist_equalized, CV_BGR2YCrCb);

    split(img_hist_equalized,channels);
    equalizeHist(channels[0], channels[0]);

    merge(channels,img_hist_equalized);

    cvtColor(img_hist_equalized, img_hist_equalized, CV_YCrCb2BGR);

    //create windows
    namedWindow("Original Image", CV_WINDOW_AUTOSIZE);
    namedWindow("Histogram Equalized", CV_WINDOW_AUTOSIZE);

    //show the image
    imshow("Original Image", img);
    imshow("Histogram Equalized", img_hist_equalized);

    waitKey(0); //wait for key press

    destroyAllWindows(); //destroy all open windows
}
